package com.bytedance.user.common;

import com.bytedance.user.http.Choices;
import com.bytedance.user.http.ToAi;
import com.bytedance.user.http.User;
import com.bytedance.user.http.ZhiPu;
import com.bytedance.user.properties.ZhiPuAiProperties;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class AIChatUtil {
    private AIChatUtil() {
    }

    public static String getSignature(ZhiPuAiProperties zhiPuAiProperties, RestTemplate restTemplate) {
        //调用api，查询文档
        ToAi toAi = new ToAi();
        User user = new User();
        user.setContent(zhiPuAiProperties.getSignature());
        List<User> users = new ArrayList<>();
        users.add(user);
        toAi.setPrompt(users);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.add("Authorization", zhiPuAiProperties.getToken());
        HttpEntity<ToAi> toAiHttpEntity = new HttpEntity<>(toAi, httpHeaders);
        ZhiPu zhiPu = restTemplate.postForObject(zhiPuAiProperties.getUrl(), toAiHttpEntity, ZhiPu.class);
        log.error("SendZhiPuChatEvent执行啦");
        List<Choices> choices = zhiPu.getData().getChoices();
        String content = choices.get(0).getContent();
        return content;
    }
}
